package test;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

import lombok.val;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class LISExcelHandler {

    private static final String excelFilePath = "D:\\Desktop\\阶段3数据库表划分V1.14.xlsx";
    private static final String projectPath = "D:\\Desktop\\D\\工作\\中科软\\code\\LIS7";
    private static final HashMap<String, List<ExcelTerm>> excelTermMap = new HashMap<>();
    private static final List<String> modelNames = new ArrayList<>();
    private static final HashMap<String, List<String>> filesMap = new HashMap<>();

    public static void main(String[] args) throws IOException {
        readExcel();

        getAllModelNames(projectPath);

        System.out.println("Start to get all DB files...");
        for (String modelName : modelNames) {
            String fixedName = projectPath + "\\" + modelName;
            List<String> filePath = new ArrayList<>();
            List<String> fileName = new ArrayList<>();
            findFile(new File(fixedName), filePath, fileName);
            filesMap.put(modelName, fileName);
        }
        System.out.println("done.");

        System.out.println("start judge");
        judge();
        System.out.println("done.");


    }

    public static void readExcel() throws IOException {
        System.out.println("Start to read excel...");
        InputStream in = Files.newInputStream(Paths.get(excelFilePath));
        Workbook workbook = new XSSFWorkbook(in);
        Sheet sheet = workbook.getSheetAt(0);

        for (int i = 1; i < sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (!excelTermMap.containsKey(row.getCell(0).toString().toLowerCase())) {
                excelTermMap.put(row.getCell(0).toString().toLowerCase(), new ArrayList<>());
            }
            List<ExcelTerm> excelTermList = excelTermMap.get(row.getCell(0).toString().toLowerCase());
            ExcelTerm term = new ExcelTerm(String.valueOf(i + 1),
                    row.getCell(0).toString().toLowerCase(),
                    row.getCell(3).toString().toLowerCase(),
                    row.getCell(10).toString().toLowerCase(),
                    row.getCell(11).toString().toLowerCase());
            excelTermList.add(term);
            excelTermMap.put(row.getCell(0).toString().toLowerCase(),
                    excelTermList);
        }

        System.out.println("done.");
    }

    public static void getAllModelNames(String path) {
        System.out.println("Start to get all model names...");
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < Objects.requireNonNull(tempList).length; i++) {
            if (tempList[i].isDirectory()) {
                if (tempList[i].getName().startsWith("lis")) {
                    modelNames.add(tempList[i].getName());
                }
            }
        }
        System.out.println("done.");
    }

    public static void findFile(File dir, List<String> filePath, List<String> fileName) {
        if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
            return;
        }
        String[] files = dir.list();// 读取目录下的所有目录文件信息
        if (files == null) return;
        for (String s : files) {// 循环，添加文件名或回调自身
            File file = new File(dir, s);
            if (file.isFile()) {// 如果文件
                if (file.getName().endsWith("DB.java")) {
                    filePath.add(dir + "\\" + file.getName());
                    fileName.add(file.getName().replace("DB.java", "").toLowerCase(Locale.ROOT));
                }
            } else {// 如果是目录
                findFile(file, filePath, fileName);// 回调自身继续查询
            }
        }
    }

    public static void judge() {
        for (String modelName : modelNames) {
            System.out.println(modelName + " error with those lines: ");
            List<ExcelTerm> modelList = excelTermMap.get(modelName);
            List<String> files = filesMap.get(modelName);
            for (ExcelTerm term : modelList) {
                if (!files.contains(term.getTableName())) {
                    System.out.println(term.getIndex());
                }
            }
        }
    }
}

@Data
@AllArgsConstructor
@ToString
class ExcelTerm {
    private String index;
    private String belongTo;
    private String tableName;
    private String current;
    private String schemaPosition;
}